<?php

namespace app\model\goods;

use app\base\BaseModel;

/**
 * 模型-GoodsTypeModel
 * Class GoodsTypeModel
 * @package app\model
 */
class GoodsAttributeValueModel extends BaseModel
{
    protected $name = 'goods_attribute_value';

    /**
     * 添加规格值
     * @param $id
     * @param $values
     * @return bool
     * @throws \Exception
     */
    public function addValue($id, $values)
    {
        if (empty($values)) {
            return false;
        }
        $all_value_data = [];
        foreach ($values as $value) {
            $all_value_data[] = [
                'attribute_id' => $id,
                'name' => $value
            ];
        }
        $ret = $this->saveAll($all_value_data);
        if ($ret !== false) {
            return true;
        }
        return false;
    }

    /**
     * 获取规格值数组列表  数组键是规格值的id
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getListArr($id)
    {
        $list = $this->where(['attribute_id' => $id])->select();
        $return_list = [];
        if (!empty($list)) {
            foreach ($list as $value) {
                $value['selected'] = false;
                $value['disabled'] = false;
                $return_list[] = $value;
            }
        }
        return $return_list;
    }

}
